package cp.remote.network;


import java.io.*;
import java.net.*;
import cp.remote.config.*;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.Log;

public class KinectImageWorker extends Network {

    private volatile boolean running = false;
    private volatile boolean connected = false;
    private Thread tp;
    private int index = 0, width, height;
    private Bitmap img = null;
    private static String name = "Kinect Imageworker";

    public KinectImageWorker() {
        super(name);
        Log.i(name, "Created");
    }

    public boolean start() {
        running = true;
        this.tp = new Thread(this);
        tp.start();
        Log.i(name, "Startet");
        return true;
    }

    public void stop() {
        running = false;
        Log.i(name, "Stop");
    }

    public synchronized Bitmap getImg() {
        return img;
    }

    public boolean isRunning() {
        return running;
    }

    public synchronized boolean isConnected(){ return connected;}
    
    private synchronized void setImg(Bitmap img) {
        this.img = img;
    }

    public void run() {
        tcprun();
    }

    public void tcprun() {
        Socket sock = null;
        InputStream in = null;
        
        try {
            connected = false;
            InetSocketAddress adr = new InetSocketAddress(NetworkConfig.ip, NetworkConfig.kinectport);
            sock = new Socket();
            sock.connect(adr, NetworkConfig.timeout);
            in = sock.getInputStream();
            connected = true;
            width = KinectConfig.imgwidth;
            height = KinectConfig.imgheight;
            
            while (running) {
                int[] imgdata = new int[width * height];
                int bytetemp = 0;

                for(int i = 0; i < imgdata.length; i++){
                    imgdata[i] |= 0xff << 24;
                    for(int k = 0; k < 3; k++){
                        bytetemp = in.read();
                        
                        if(bytetemp < 0){ Log.w(name, "TCP Stream to short"); break;}
                        imgdata[i] |= bytetemp << (8 * k);
                    }
                }
                setImg(Bitmap.createBitmap(imgdata, width, height, Config.ARGB_8888));
            }
        } catch (UnknownHostException e) {
           error(e.getMessage());
        } catch (IOException e) {
            error(e.getMessage());
        } finally {
            Log.i(name, "Shutdown");
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
            try {
                if (sock != null)
                    sock.close();
            } catch (IOException e) {
            }
        }
    }

    public void udprun() {

        try {

            DatagramSocket socket = new DatagramSocket(NetworkConfig.kinectport);

            while (running) {
                int[] img = new int[width * height];
                byte[] data = new byte[width * 4 * 3];
                index = 0;
                for (int i = 0; i < 120; i++) {
                    DatagramPacket recive = new DatagramPacket(data,
                            data.length);
                    socket.receive(recive);

                    for (int k = 0; k < data.length; k += 3) {
                        int temp = 0;
                        temp |= (0xff) << 24;
                        temp |= data[k] << 16;
                        temp |= data[k + 1] << 8;
                        temp |= data[k + 2];
                        img[index] = temp;
                        index++;
                    }
                }

                setImg(Bitmap.createBitmap(img, width, height, Config.ARGB_8888));
            }

        } catch (SocketException e) {
            error(e.getMessage());
        } catch (IOException e) {
            error(e.getMessage());
        }
    }


}
